﻿using AutoMapper;
using Entity.WMS.DTO.WarehouseBalance;
using Entity.WMS.View.WarehouseBalance;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.WMS
{
    public class WarehouseBalanceService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public WarehouseBalanceService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取库存结存单列表
         */
        public async Task<RequestResponseModel<PageResult<WarehouseBalanceView>>> GetWarehouseBalanceList(WarehouseBalanceSearchDTO warehouseBalanceSearchDTO)
        {
            //筛选
            var query = _context.WarehouseBalanceView
                .WhereIF(a => a.GoodsCode.Contains(warehouseBalanceSearchDTO.Goods) || a.GoodsName.Contains(warehouseBalanceSearchDTO.Goods) || a.GoodsSpec.Contains(warehouseBalanceSearchDTO.Goods), !string.IsNullOrEmpty(warehouseBalanceSearchDTO.Goods))
                ;

            //返回列表
            var warehouseBalanceList = await query
                .Page(warehouseBalanceSearchDTO.CurrentPage, warehouseBalanceSearchDTO.PageSize)
                .ToListAsync();

            //计算总数
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<WarehouseBalanceView>(warehouseBalanceList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 获取库存结存单子单列表
         */
        public async Task<RequestResponseModel<PageResult<WarehouseBalanceItemView>>> GetWarehouseBalanceItemList(WarehouseBalanceItemSearchDTO warehouseBalanceItemSearchDTO)
        {
            //筛选
            var query = _context.WarehouseBalanceItemView
                .Where(a => a.WarehouseBalanceId == warehouseBalanceItemSearchDTO.Id)
                ;

            //返回列表
            var warehouseBalanceItemList = await query
                .OrderBy(a => a.CreateTime)
                .Page(warehouseBalanceItemSearchDTO.CurrentPage, warehouseBalanceItemSearchDTO.PageSize)
                .ToListAsync();

            //计算总数
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<WarehouseBalanceItemView>(warehouseBalanceItemList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }
    }
}
